<?php
class npcModel extends appModel{
    public function getHomeInfo()
    {
        $sql  = "SELECT place,url FROM ".$this->table("npc_home");
        return $this->getAll($sql);
    }
    
    public function getOldestNpc($id)
    {
        $where = empty($id)?"":" AND id = $id ";
        $sql="SELECT id,img_url,title,content,comment_num,forward_num,see_num,like_num,create_time,des_content FROM ".$this->table("npc")." WHERE 1 = 1 $where order by create_time desc LIMIT 1 ";
        $result=$this->getRow($sql);
        if($result)
        {
            $where = " p1.status = 1 AND p1.nid = ".$result['id']." ORDER BY create_time DESC LIMIT 20";
            $sql="SELECT p1.id,p2.headimgurl,p2.nickname,p1.comment FROM ".$this->table('npc_comment')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
            $comment_list = $this->getAll($sql);
            foreach ($comment_list as $k=>$v)
            {
                if(preg_match("/\/0$/", $v['headimgurl']))
                {
                    $comment_list[$k]['headimgurl']=substr($v['headimgurl'],0,strlen($v['headimgurl'])-1)."64";
                }
            }
            $sql = "SELECT is_liked FROM ".$this->table("npc_liked")." WHERE nid=".$result['id']." AND wxid = '".$_SESSION['wxid']."' LIMIT 1 ";
            $status = $this->getOne($sql);
            $result['is_liked'] = $status;
            $result['comment_list'] = $comment_list;
            return $result;
        }
        else
        {
            return $result=array();
        }
    }
    
    public function zan($wxid,$nid)
    {
        $sql = "SELECT id,is_liked FROM ".$this->table('npc_liked')." WHERE wxid = $wxid AND nid = $nid LIMIT 1 ";
        $row = $this->db->getRow($sql);
        if($row)//有值
        {
            if($row['is_liked']==1)//取消
            {
                $info['is_liked'] = 0 ;
                $result['id'] = $this->update($info, array("table"=>"npc_liked","id"=>$row['id']));
                $result['codeMsg'] = "cancel";
                $this->add_sub_number("npc","like_num", "-1", $nid);
    
            }else//收藏
            {
                $info['is_liked'] = 1 ;
                $result['id'] = $this->update($info, array("table"=>"npc_liked","id"=>$row['id']));
                $result['codeMsg'] = "like";
                $this->add_sub_number("npc","like_num", "+1", $nid);
            }
        }else//无值
        {
            $info['is_liked'] = 1;
            $info['nid'] = $nid;
            $info['wxid'] = $wxid;
            $result['id'] = $this->insert($info, "npc_liked");
            $result['codeMsg'] = "like";
            $this->add_sub_number("npc","like_num", "+1", $nid);
        }
        $sql = "SELECT like_num FROM ".$this->table("npc")." WHERE id = $nid LIMIT 1";
        $result['like_num'] = $this->getOne($sql);
        return $result;
    }
    
    public function add_sub_number($table,$field,$number,$id)
    {
        $sql = "UPDATE ".$this->table($table). " SET $field = $field $number WHERE id = $id ";
        $this->query($sql);
    }
    
    public function sendComment($wxid,$comment,$nid)
    {
        $info['status'] = 0;
        $info['create_time'] = yf_time();
        $info['wxid'] = $wxid;
        $info['nid'] = $nid;
        $info['comment'] = $comment;
        $result = $this->insert($info, "npc_comment");
        if($result){
            $this->add_sub_number("npc", "comment_num", "+1", $nid);
            $user = $this->get_record($wxid);
            return array("error"=>0,"content"=>"评论成功","id"=>$result,"user"=>$user);
        }else{
            return array("error"=>1,"content"=>"评论保存失败");
        }
    }
    
    /**
     *获得一条微信record记录
     */
    public function get_record($id)
    {
        if(!$id)
        {
            return false;
        }
        $sql = "SELECT wxid,nickname,headimgurl FROM ".$this->table('wxusers')." WHERE wxid =".$id;
        $result=$this->getRow($sql);
    
        if($result)
        {
    
            return $result;
        }
        else
        {
            return false;
        }
    }
    
    public function isExitNpc($id)
    {
        if(empty($id)){
            $sql="SELECT id FROM ".$this->table("npc")." WHERE 1 = 1 order by create_time desc LIMIT 1 ";
            $nid=$this->getOne($sql);
            return $nid;
        }
        $sql = "SELECT id FROM ".$this->table("npc")." WHERE id = $id LIMIT 1 ";
        $res = $this->getOne($sql);
        if($res)
        {
            return $id;
        }else{
            $sql="SELECT id FROM ".$this->table("npc")." WHERE 1 = 1 order by create_time desc LIMIT 1 ";
			$nid=$this->getOne($sql);
			return $nid;
        }
    }
}